package cn.fl.warrant.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.itextpdf.text.pdf.PdfStructTreeController.returnType;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.warrant.service.IArcPostInfoService;
import cn.fl.warrant.transform.ArcPostInfoVOTransform;
import cn.fl.warrant.vo.ArcPostInfoVO;
import fl.warrant.facade.archive.api.IArcPostInfoApiService;
import fl.warrant.facade.archive.condition.ArcPostInfoQC;
import fl.warrant.facade.archive.dto.ArcPostInfoDTO;

/**
 * @Description: 邮寄信息(旧表：无) 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class ArcPostInfoServiceImpl implements IArcPostInfoService {

	
	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IArcPostInfoService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IArcPostInfoApiService arcPostInfoApiService;

	@Override
	public ArcPostInfoVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		ArcPostInfoDTO dto = arcPostInfoApiService.selectById(id);
		return ArcPostInfoVOTransform.toVO(dto);
	}

	@Override
	public Long add(ArcPostInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.arcPostInfoApiService.add(ArcPostInfoVOTransform.toDTO(entity));
		
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.arcPostInfoApiService.removeById(id);
		
	}
	
	

	@Override
	public boolean modify(ArcPostInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.arcPostInfoApiService.modify(ArcPostInfoVOTransform.toDTO(entity));
		
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		String sysCd=CurrentThreadContext.getCurrentSysCd();
		String orgCd=CurrentThreadContext.getCurrentOrgCd();
		return this.arcPostInfoApiService.searchListPage(page,sysCd,orgCd);
		
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public List<ArcPostInfoVO> searchList(ArcPostInfoQC qc) throws BaseException {
		return  ArcPostInfoVOTransform.toVOList(arcPostInfoApiService.searchList(qc));
	}
	/**
	 * 添加修改邮寄信息
	 * @param map
	 * @return
	 * 2018年3月30日 
	 * lxb
	 */
	@Override
	public Long addOrUpdate(Map map)throws BaseException {
		ArcPostInfoVO pVo=new ArcPostInfoVO();
		if (StringUtils.isNotBlank((String)map.get("sendPorson"))) {
			pVo.setSendPorson((String)map.get("recipient"));
		}else {
			pVo.setSendPorson(CurrentThreadContext.getCurrentUserName());
		}
		if (map.get("sendDate")!=null&&!"".equals("sendDate")) {
			pVo.setSendDate((Date)map.get("sendDate"));
		}else {
			pVo.setSendDate(new Date());
		}
		if (StringUtils.isNotBlank((String)map.get("recipient"))) {
			pVo.setRecipient((String)map.get("recipient"));
		}
		if (StringUtils.isNotBlank((String)map.get("mobile"))) {
			pVo.setMobile((String)map.get("mobile"));
		}
		if (StringUtils.isNotBlank((String)map.get("sendType"))) {
			pVo.setSendType((String)map.get("sendType"));
			if ("0".equals(map.get("sendType"))) {
				if (StringUtils.isNotBlank((String)map.get("sendCompany"))) {
					pVo.setSendCompany((String)map.get("sendCompany"));
				}
				if (StringUtils.isNotBlank((String)map.get("sendNum"))) {
					pVo.setSendNum((String)map.get("sendNum"));
				}
				if (StringUtils.isNotBlank((String)map.get("sendAddr"))) {
					pVo.setSendAddr((String)map.get("sendAddr"));
				}
			}
			
		}
		
		if (StringUtils.isNotBlank((String)map.get("idCard"))) {
			pVo.setIdCard((String)map.get("idCard"));
			
		}
		if (StringUtils.isNotBlank((String)map.get("arcAppId"))) {
			pVo.setArcAppId(Long.valueOf((String)map.get("arcAppId")));
		}
		if (StringUtils.isNotBlank((String)map.get("arcAppType"))) {
			pVo.setArcAppType((String)map.get("arcAppType"));
		}
		if (StringUtils.isNotBlank((String)map.get("arcAppBussType"))) {
			pVo.setArcAppBussType((String)map.get("arcAppBussType"));
		}
		if (StringUtils.isNotBlank((String)map.get("sendPorsonType"))) {
			pVo.setSendPorsonType((String)map.get("sendPorsonType"));
		}
		if (StringUtils.isNotBlank((String)map.get("remark"))) {
			pVo.setRemark((String)map.get("remark"));
		}
		if(map.get("postId")!=null&&!"".equals(map.get("postId"))){
			pVo.setId(Long.valueOf(map.get("postId").toString()));
			pVo.setMdfUsrId(CurrentThreadContext.getCurrentUserId());//add 修改人id by gengchunlan 20191014
			arcPostInfoApiService.modify(ArcPostInfoVOTransform.toDTO(pVo));
			return Long.valueOf(map.get("postId").toString());
		}else {
			if (StringUtils.isNotBlank((String)map.get("idCard"))) {
				pVo.setCrtUsrId(CurrentThreadContext.getCurrentUserId());//add 创建人id by gengchunlan 20191014
				return arcPostInfoApiService.add(ArcPostInfoVOTransform.toDTO(pVo));
			}else {
				return null;
			}
		}
		
	}
}
